//
//  NPSearchMaskView.m
//  newProject
//
//  Created by Mac mini on 2017/10/13.
//  Copyright © 2017年 zt. All rights reserved.
//

#import "NPSearchMaskView.h"

@implementation NPSearchMaskView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        [self createClearHistory];
        
        [self createHotView];
    }
    return self;
}

-(void)createClearHistory{
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 20)];
    lab.text = @"搜索历史";
    lab.font = [UIFont systemFontOfSize:14];
    [self addSubview:lab];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(ACEWidth-80, 20, 70, 20)];
    [button addTarget:self action:@selector(clearHistory) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:@"清空记录" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:button];
}

-(void)createHotView{
    _hotButtonView = [[NPHotSearchView alloc]initWithFrame:CGRectMake(0,60, ACEWidth, ACEHeight- 150)];
    [self addSubview:_hotButtonView];
}

-(void)clearHistory{
//    NSMutableArray *array = [BFPublicDefTool readDataFromPlist:@"searchHistory" andWillBackType:@"NSArray"];
//    if (array == nil) {
//        array = [NSMutableArray arrayWithObjects:str, nil];
//    }else{
//        for (int i = 0; i<array.count; i++) {
//            if ([array[i] isEqualToString:str]) {
//                break;
//            }else{
//                if (i == array.count-1) {
//                    [array addObject:str];
//                }
//            }
//        }
//        
//    }
    
    
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:@"searchHistory.plist"];
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
    array = nil;
    [self.hotButtonView reloadHistory];
    
//    array = nil;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
